package com.xinli.xinlifm.view.adapter.find;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.find.FindXinChangBean;

import java.util.Collection;
import java.util.List;

/**
 * Created by zz on 16-10-13.
 */

public class FindSecondRecyclerAdapter extends RecyclerView.Adapter<FindSecondRecyclerAdapter.ViewHolder> {

    private final LayoutInflater inflater;
    private Context context;
    private List<FindXinChangBean.DataBean> list;
    private OnChildItemListener onChildItemListener;
    private FindSecondRecyclerAdapter.ViewHolder viewHolder;

    public FindSecondRecyclerAdapter(Context context, List<FindXinChangBean.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    @Override
    public FindSecondRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(viewType, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FindSecondRecyclerAdapter.ViewHolder holder, final int position) {
        if(position==list.size()){
            return;
        }
        final FindXinChangBean.DataBean dataBean = list.get(position);
        viewHolder = holder;
        if (getItemViewType(position) == R.layout.find_second_item) {
            int viewnum = dataBean.getViewnum();
            holder.viewNum.setText(String.valueOf(viewnum));
            Picasso.with(context).load(dataBean.getCover()).placeholder(R.mipmap.default_1).error(R.mipmap.umeng_socialize_share_music).into(holder.cover);
            holder.title.setText(dataBean.getTitle());
            holder.speak.setText(dataBean.getSpeak());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onChildItemListener.onChildItemListener(dataBean,position);
                }
            });
        }
    }

    public void addAll(Collection collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {
            return R.layout.download;
        } else {
            return R.layout.find_second_item;
        }
    }

    public void setOnChildItemListener(OnChildItemListener onChildItemListener){
        this.onChildItemListener = onChildItemListener;
    }

    public void setLastShow(int i) {
        switch (i){
            case 1:
                viewHolder.layout.setVisibility(View.VISIBLE);
                viewHolder.textView.setVisibility(View.INVISIBLE);
                break;
            case 2:
                viewHolder.layout.setVisibility(View.INVISIBLE);
                viewHolder.textView.setVisibility(View.VISIBLE);
                break;
        }
    }

    public interface OnChildItemListener {
        void onChildItemListener(FindXinChangBean.DataBean dataBean,int position);
    }
    public void clear(){
        list.clear();
        notifyDataSetChanged();
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        LinearLayout layout;
        LinearLayout itemLayout;
        ImageView cover;
        TextView title;
        TextView speak;
        TextView viewNum;

        public ViewHolder(View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.find_list_item_cover);
            title = (TextView) itemView.findViewById(R.id.find_list_item_title);
            speak = (TextView) itemView.findViewById(R.id.find_list_item_speak);
            viewNum = (TextView) itemView.findViewById(R.id.find_list_item_viewnum);
            itemLayout = ((LinearLayout) itemView.findViewById(R.id.find_second_item_ll));
            layout = ((LinearLayout) itemView.findViewById(R.id.one));
            textView = ((TextView) itemView.findViewById(R.id.two));
        }
    }
}
